package com.huobao.yunying.activitys;

import java.util.List;
import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_Ketang_more;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.KetangMore;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.zhangying.R;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 更多课程
 * 
 * @author Administrator
 *
 */
public class Act_Ketang_More extends HuobaoBaseActivity implements OnScrollListener, OnItemClickListener {

	@ViewInject(R.id.listview)
	private ListView listview;
	private Adapter_Ketang_more adapter;
	private View Headiew;
	private View footView;
	private TextView tv_message;
	private int m_CurrentPage = 1;
	private boolean isLoading = false;

	@Override
	protected int getContentView() {
		return R.layout.layout_ketang_more;
	}

	@Override
	protected String getTitleText() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void init() {
		mAbTitleBar.setVisibility(View.GONE);
		footView = LayoutInflater.from(context).inflate(R.layout.item_footview, null);
		tv_message = (TextView) footView.findViewById(R.id.tv_message);
		Headiew = LayoutInflater.from(context).inflate(R.layout.item_ketang_headview, null);
		listview.setOnScrollListener(this);
		listview.setOnItemClickListener(this);
		listview.addHeaderView(Headiew);
		listview.addFooterView(footView);
		adapter = new Adapter_Ketang_more(context);
		listview.setAdapter(adapter);
		getMore(true);
	}

	private void getMore(boolean isshowDialog) {
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, "ketang", HttpConst.SECOND_KEY, "kechmore",
				"page", m_CurrentPage + "");
		Http.getInstant().sendRequest(context, isshowDialog, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				List<KetangMore> list = JSON.parseArray(jsonObject.getString("retval"), KetangMore.class);
				if (list == null || list.size() == 0) {
					isLoading = true;
					tv_message.setText("没有更多了");
					return;
				}
				adapter.addAll(list);
				isLoading = false;
			}

			@Override
			public void onHttpError(String error) {
			}
		});
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if (isLoading || totalItemCount == 0 || firstVisibleItem == 0)
			return;
		if ((firstVisibleItem + visibleItemCount) >= (totalItemCount - 1)) {
			m_CurrentPage++;
			isLoading = true;
			getMore(false);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if (position == adapter.getCount() - 2 || position == 0)
			return;
		Intent intent = new Intent(this, Act_Ketang_detail.class);
		intent.putExtra("data", adapter.getList().get(position - 1));
		startActivity(intent);
	}
}
